/**
 * @FileName: autoSelect.js
 * @Summary: 下拉选择框
 * @Encoding: UTF-8
 * @Author shixy
 * @Version: 1.0
 * @Date: 2009-6-16
 */
/* 输入联想模式 */
if (ProjConfigParam.ASSOCIATE_MODE == 'INPUT') {
	(function($) {
		$.fn.autoSelect = function(config) {
			return this.each(function() {
				/**
				 * 防止重复初始化
				 */
				if(this.completed){return;}
				
				if(!$(this).hasClass("associateDefault")){
					$(this).addClass("associateDefault");
				}
				
				$(this).attr("autocomplete", "off");
				config.id = this.id;
				config.currentDomSelect = "#"+this.id;
				config.ajaxDivId = "autoSelectDiv" + this.id;
				config.width = config.width || 350;
				config.filter = config.filter || "";
				config.characterNum = config.characterNum || 1;
				config.condition = config.condition || "likeeic";
				config.searchKeyCode = config.searchKeyCode || 13;// 默认回车查询
				config.whereCondition = config.whereCondition || "";
				config.index = -1;
				config.imageDivId = this.id + "_image_associate";
				config.commonServiceName = config.commonServiceName || "commonService";
				config.inputValueAgain = false;   // 标识选择后是否重新输入
				config.timeObj = null;
//				config.url = '';
				
				config.ajaxPara = {
					className : config.className,
					sortFields : config.sortFields || "",
					fieldNames : config.displayField || "",
					queryField : config.queryField || "",
					displayNum : config.displayNum < 1 ? 10 : config.displayNum,
					filter : config.filter || "",
					parentFieldName : config.parentFieldName || "",
					displayFieldI18n : config.displayFieldI18n || "", // 列头国际化配置
																		// filipo
																		// 20100630
					displayValueMap : config.displayValueMap ||"", // 显示内容转换
																	// filipo
																	// 20100630
					isWhereCondition : config.isWhereCondition || "false", // 依赖whereCondition
																			// 组装的条件标识(20110113xiezm)[针对Fros
																			// 对复杂条件组装不支持，如：多字段的and、or]
					commonServiceName : config.commonServiceName
				};

				config.query = true; // 默认按回车查询 add by liaoqs
				config.isAjaxDivHide = true; // 标识div层是否隐藏 gxl 20090826
				config.isFromSelect = false; // 标识是否选择了下拉框中的数据 filipo
												// 20100304
				config.thisId = this.id;
				config.keyupTimeobj = null;

//				$(this).keydown(function(e) {
//					tabKeyCloseDiv(e, config);
//				});
				
				if ($.browser.msie){
					this.attachEvent("onkeydown",function(e){
						tabKeyCloseDiv(e,config);
					});
				}else{
					this.addEventListener('keydown', function(e){
						tabKeyCloseDiv(e,config);
					}, true);
				}

				$(this).keyup(function(e){
					// 防止立即加载
					window.clearTimeout(config.keyupTimeobj);
					var kc = e.keyCode;
					
					
					switch(kc){
						case 40:
						case 38:
						
							if(config.isAjaxDivHide) break;
							
							var tableSize = $("#" + config.ajaxDivId + " tr").size() - 1;
							if (kc == 40) {
								if (config.index == tableSize - 1)
									config.index = 0;
								else
									config.index++;
							}else{
								if (config.index == -1)
									config.index = tableSize - 1;
								else
									config.index--;
							}
							$("#" + config.ajaxDivId + " tr.autoHighlight").trigger("mouseout");
							$("#" + config.ajaxDivId + " tr.odd:eq(" + config.index + ")").trigger("mouseover");
							break;
						case 9:
							break;
						case config.searchKeyCode:
						case 13:
						
							// 增加按回车键后选择文本，add by liuzg.
							if (!config.query && kc == 13 || (config.searchKeyCode != 13 && kc == 13)) {
								if (config.index != -1) {
									var domObj = $(config.currentDomSelect + "AutoSelectTableId tr.autoHighlight").get(0);
									if (domObj) 
										setInputValue(domObj, config);
									else {
										// $(this).val("");
										closeDiv($("#" + config.ajaxDivId), config);
									}
									config.index = -1;
								}
								else {
									// $(this).val("");
								}
								config.query = true;
								// config.isFromSelect = true; // filipo// 20100304
								// config.isAjaxDivHide = true; // gxl// 20090826
							}
							else {
								getSelectHtml(config);
								return false;
							}
							
							break;
						default:
							config.keyupTimeobj = window.setTimeout(function(){
								if (config.keyupTimeobj == null) {
									return;
								}
								if (!(this.readOnly != null && this.readOnly)) { // 控件readonly属性设为true时,
									if (config.characterNum &&
									$(config.currentDomSelect).val() != undefined &&
									$(config.currentDomSelect).val().length >= config.characterNum) 
										getSelectHtml(config);
									if ($(config.currentDomSelect).val() == undefined || $(config.currentDomSelect).val() == '') {
										closeDiv($("#" + config.ajaxDivId), config);
									}
									config.query = true;
								}
							}, 1000);
							break;
					}
				
			
				}).change(function() {
					config.isAjaxDivHide = false;
					if(config.canNotInput === true){
						config.isFromSelect = false;
						config.inputValueAgain = true;
					}
					if ($.trim(this.value) == "") {
						$("#" + config.hiddenId).val("");
						$(config.syncClearControls).val("");
					}
				});
				// 如果限制为不可以输入 gxl 20090826
//				$(this).blur(function(){
					
//					if(config.canNotInput && $(this).val().length != 0){
//						// 点击行的config.isFromSelect
//						// 和下拉层的config.isAjaxDivHide
//						// 和当前联想控件输入框 的失焦 事件同时 触发
//						// 导致有时间差 需要延迟判断是否清空输入的值
//						function _cleanValue(){
//							
//							var $currentDomSelect = $(config.currentDomSelect);
//							
//							if ($currentDomSelect.length == 0) {
//							
//								if(config.timeObj)window.clearInterval(config.timeObj);
//							}
//							else {
//							
//								if ($currentDomSelect.val() &&
//								$currentDomSelect.val().length > 0 &&
//								config.isFromSelect === false &&
//								config.isAjaxDivHide === true &&
//								config.inputValueAgain === true) {
//									config.isFromSelect == true;
//									$(config.currentDomSelect).val("")
//									$(config.currentDomSelect).trigger("change");
//									window.clearInterval(config.timeObj);
//									config.timeObj = null;
//								}
//								else 
//									if (config.keyupTimeobj != null &&
//									!$.StringUtils.isEmpty($currentDomSelect.val()) &&
//									config.isFromSelect === false &&
//									config.inputValueAgain === true) {
//										$currentDomSelect.val("")
//										$currentDomSelect.trigger("change");
//										window.clearTimeout(config.keyupTimeobj);
//										window.clearInterval(config.timeObj);
//										config.timeObj = null;
//									}
//									else {
//										if ($currentDomSelect.val().length == 0) {
//											window.clearInterval(config.timeObj);
//											config.timeObj = null;
//										}
//									}
//							}
//						}
//				
//						if(config.timeObj == null)
//							config.timeObj = window.setInterval(_cleanValue,50);
//					}
//				});
				

					// 单击联想下拉图标时下拉
				
						$("#" + config.imageDivId).click(function() {
							
							if (!this.disabled) {// 控件disabled的时候，点击事件不可用,
												// 但是设为disabled后,控件里面的值不能在表单中提交.
								if (!this.readOnly) { // 增加条件判断,否则当控件readonly属性设为true时,还会有点击事件.
													// added by filipo 20100319
									getSelectHtml(config);
									$(config.currentDomSelect).focus();
								}
							}
						});
					$("#" + config.imageDivId).keydown(function(e) {
						
						if (!this.disabled) {
							tabKeyCloseDiv(e, config);
						}
					});

// if (!this.readOnly) {// 控件readOnly的时候，点击事件不可用 added by
// // filipo 20100319
// $("#" + config.imageDivId).click(function() {
// if (!this.readOnly) {
// getSelectHtml(config);
// }
// });
// }

				var hiddenField = $("#" + config.hiddenId);
				/*
				 * hiddenField.hide(); if(config.showHiddenField){
				 * hiddenField.show();
				 * hiddenField.attr("style","background-color:
				 * #EFEFEF;width:35px;margin-left:-6px;")
				 * hiddenField.attr("readonly","true");
				 * $(this).attr("style","width:85px"); }
				 * if(config.loadRecord){//以下为了编缉的时候为框付值 var input =
				 * $("#"+config.hiddenId)[0]; if(input){
				 * CommonService.findByProperty(config.className,input.name,input.value,function(obj){
				 * if( obj[0] && config.currentDom ) config.currentDom.value =
				 * eval("obj[0]."+config.currentDom.name); }); } }
				 */
				if (config.inTable) {// 保存数据
					var queryInfo = {
						listType : 'list',
						conjunctionType : 'and',
						queryObjectName : config.className
					};
					var fields = [];
					var filterFields = config.filter.split(",");
					for ( var index = 0; index < filterFields.length; index++) {
						var filterField = filterFields[index].split("|");
						var field = {};
						field.condition = "eq";
						field.fieldName = filterField[0];
						field.fieldValueString = filterField[1];
						fields[fields.length] = field;
					}
					queryInfo.fields = fields;
					eval($.StringUtils.firstUpperCase(config.commonServiceName))["findByQuery"](queryInfo, function(result) {
						$(config.currentDomSelect)[0].source = result;
					});
				}
				if (window.addEventListener) {
					if (hiddenField[0])
						hiddenField[0].addEventListener('DOMAttrModified',
								function() {
									$(config.currentDomSelect).attr("title",this.value);
								}, false);
				} else {
					if (hiddenField[0]) {
						hiddenField[0].attachEvent("onpropertychange",
								function() {
									$(config.currentDomSelect).attr("title",this.value);
								});
					}
				}
				
				this.config = config;
				this.completed = true;
			});
		};

		/**
		 * ajax请求，从后台获取需要的数据
		 */
		function getSelectHtml(config) {
			if (config.ajaxPara.fieldValue !== $(config.currentDomSelect).val()) {
				config.ajaxPara.currPage = 1;
			}
			if (document.getElementById(config.parentId)
					&& document.getElementById(config.parentId).value != ''
					// &&
					// document.getElementById(config.parentId+"_"+config.parentHiddenId))
					&& document.getElementById(config.parentHiddenId)) {
				// config.parentHiddenValue =
				// $("#"+config.parentId+"_"+config.parentHiddenId).val();
				config.parentHiddenValue = $("#" + config.parentHiddenId).val();
				config.ajaxPara.parentFieldValue = config.parentHiddenValue;
			} else {
				config.ajaxPara.parentFieldValue = '';
			}
			config.ajaxPara.selectId = $(config.currentDomSelect).attr("id");
			config.ajaxPara.fieldValue = $(config.currentDomSelect).val();
			config.ajaxPara.condition = config.condition;
			if (config.whereConditionFunc) {// 添加返回whereCondition函数接口 Added by
											// Cloud
				config.ajaxPara.whereCondition = eval(config.whereConditionFunc + "()");
			} else {
				config.ajaxPara.whereCondition = config.whereCondition;
			}
			/* 配置列宽 Start */// Added by Cloud
			if (config.colWidths) {
				config.ajaxPara.colWidths = config.colWidths;
			} else {
				var colWidthStr = "";
				var columnL = config.displayField.split(",").length;
				for ( var i = 0; i < columnL; i++) {
					colWidthStr += (config.width / columnL) + ",";
				}
				// colWidthStr = colWidthStr.substring(0,colWidthStr.length -
				// 1);
				config.ajaxPara.colWidths = colWidthStr;
			}
			/* 配置列宽 End */

			startLoading(config);
			$.ajax( {
						type : "POST",
						url : config.url + "/autoSelect.do?method=auto",
						timeout : 20000,
						error : function(response) {
							errorPrompt(config,response);
						},
						data : config.ajaxPara,
						success : function(msg) {
							// 用户被踢后,ajax请求时回到登录页面 2010-04-14 jcc
							if (msg.indexOf('login_page_unique_flag') != -1) {
								window.top.document.location.href = App.ctx + '/login.jsp';
								return;
							}
							openDiv(config, msg);
							closeLoading(config);
							if (config.colWidths) {
								var widths = config.colWidths.split(",");
								var headerRows = $(config.currentDomSelect+ "AutoSelectTableId td.tableHeader");
								headerRows.each(function(index, domObj) {
									$(domObj).width(widths[index]);
								});
							}
							var rows = $(config.currentDomSelect + "AutoSelectTableId tr.odd");
							rows.each(function(index, domObj) {
								$(domObj).bind("click", function() {
									setInputValue(domObj, config);
									config.isFromSelect = true; // filipo
																// 20100304
									});
							});
							$("#" + config.ajaxDivId + " tr.odd:eq(0)").trigger("mouseover");
							var totalNum = $(config.currentDomSelect + "totalNumId").val();
							if (parseInt(totalNum) && parseInt(totalNum) > 0)
								config.index = 0;
							else
								config.index = -1;

							bindEvent(config);
							setImgDisabled(config);
						}
					});
		}
		;

		/**
		 * 分页后事件
		 */
		function setImgDisabled(config) {
			if (config.currPage == 1) {
				$(config.currentDomSelect + "firstPage").attr("src",config.url+"/scripts/widgets/autoSelect/images/firstPageDisabled.gif");
				$(config.currentDomSelect + "firstPage").attr("title", "");
				$(config.currentDomSelect + "firstPage").attr("alt", "");
				$(config.currentDomSelect + "prevPage").attr("src",config.url+"/scripts/widgets/autoSelect/images/prevPageDisabled.gif");
				$(config.currentDomSelect + "prevPage").attr("title", "");
				$(config.currentDomSelect + "prevPage").attr("alt", "");
			}
			if (config.currPage == config.totalPage) {
				$(config.currentDomSelect + "nextPage").attr("src",config.url+"/scripts/widgets/autoSelect/images/nextPageDisabled.gif");
				$(config.currentDomSelect + "nextPage").attr("title", "");
				$(config.currentDomSelect + "nextPage").attr("alt", "");
				$(config.currentDomSelect + "lastPage").attr("src",config.url+"/scripts/widgets/autoSelect/images/lastPageDisabled.gif");
				$(config.currentDomSelect + "lastPage").attr("title", "");
				$(config.currentDomSelect + "lastPage").attr("alt", "");
			}
		}

		/**
		 * 绑定分页事件
		 */
		function bindEvent(config) {
			var currPage = $(config.currentDomSelect + "currPageId");
			var totalPage = $(config.currentDomSelect + "totalPageId");
			var totalNum = $(config.currentDomSelect + "totalNumId");
			if (currPage[0])
				config.currPage = parseInt(currPage[0].value);
			if (totalPage[0])
				config.totalPage = parseInt(totalPage[0].value);
			if (totalNum[0])
				config.totalNum = parseInt(totalNum[0].value);
			if (config.currPage != 1) {
				$(config.currentDomSelect + "firstPage").click(function(e) {
					config.ajaxPara.currPage = 1;
					getSelectHtml(config);
				});
				$(config.currentDomSelect + "prevPage").click(function(e) {
					config.ajaxPara.currPage = config.currPage - 1;
					getSelectHtml(config);
				});
			}
			if (config.currPage != config.totalPage) {
				$(config.currentDomSelect + "nextPage").click(function(e) {
					config.ajaxPara.currPage = config.currPage + 1;
					getSelectHtml(config);
				});
				$(config.currentDomSelect + "lastPage").click(function(e) {
					config.ajaxPara.currPage = config.totalPage;
					getSelectHtml(config);
				});
			}
			var fun = null;
			$(config.currentDomSelect + "gotoPageInput").bind("focus",
					function(event) {
						fun = document.onclick;
						document.onclick = null;
					});
			$(config.currentDomSelect + "gotoPageInput").bind("blur",
					function(event) {
						document.onclick = fun;
					});

			$(config.currentDomSelect + "gotoPageInput").bind(
					"keyup",
					function(event) {
						var e = event || window.event;
						if (e.keyCode == 13) {
							$(config.currentDomSelect + "gotoPage").trigger("click");
						}
					});

			$(config.currentDomSelect + "gotoPage").click(
					function(e) {
						var value = $(config.currentDomSelect + "gotoPageInput")[0].value;
						if (value) {
							if (parseInt(value) != config.currPage
									&& parseInt(value) > 0
									&& parseInt(value) <= config.totalPage) {
								config.ajaxPara.currPage = value;
								getSelectHtml(config);
							}
						}
					});
		}

		/**
		 * 打开下拉框内容
		 */
		function openDiv(config, msg) {
			var obj = $(config.currentDomSelect)[0];
			var divId = config.ajaxDivId;
			var width = config.width;
			var opDiv = document.getElementById(divId);
			var offset = $(obj).offset();
			if (!opDiv) {
				opDiv = document.createElement('div'); // 创建层
				opDiv.id = divId;
				document.body.appendChild(opDiv);
			}
			$("#" + config.ajaxDivId).html(msg);
			var w = document.body.offsetWidth - offset.left;
			var left = null;
			if (w < width) {
				left = offset.left + obj.offsetWidth - width;
			} else {
				left = offset.left;
			}
			var bottomDistance = document.documentElement.clientHeight - offset.top;
			var topDistance = offset.top - obj.offsetHeight;
			var gridObj = document.getElementById(config.ajaxDivId);
			var top = offset.top + $(obj).height() + 5;
			if (gridObj.offsetHeight + 5 > bottomDistance
					&& topDistance > gridObj.offsetHeight)
				top = offset.top - gridObj.offsetHeight - 3;
			$(opDiv).css("left", left + "px");
			$(opDiv).css("top", top + "px");
			$(opDiv).css("width", width + "px");
			opDiv.style.zIndex = '9999';
			opDiv.style.position = 'absolute';
			opDiv.style.cursor = 'default';
			opDiv.style.overflow = 'hidden';
			opDiv.style.border = '1px solid #000';
			document.onclick = function(e) {
				// $(config.currentDomSelect + "gotoPageInput").val("");
				OnDocClick(e, $(obj), $(opDiv), config);
			};
			
			/* liucy end 2011-9-9 */
			config.query = false;
			config.isAjaxDivHide = false; // filipo 20100304
			//多tab点击需要关闭
			$("a").bind("click",function(e){
				checkRecordExist(config);
				OnDocClick(e, $(obj), $(opDiv), config);
			});
		}

		/**
		 * 关闭下拉框
		 */
		function closeDiv(displayDiv, config) {
			$(displayDiv).css("left", "-9999px");
			$(displayDiv).css("top", "-9999px");
			config.isAjaxDivHide = true; // filipo 20100304
		}

		/**
		 * 打开加载图片层
		 */
		function startLoading(config) {
			var obj = $(config.currentDomSelect)[0];
			var offset = $(obj).offset();
			var top = offset.top + $(obj).height() + 5;
			var left = offset.left;
			var loadingDiv = document
					.getElementById(obj.id + 'LoadingDiv');
			if (!loadingDiv) {
				loadingDiv = document.createElement('div');
				loadingDiv.id = obj.id + 'LoadingDiv';
				document.body.appendChild(loadingDiv);
			}
			$(loadingDiv).html("<img src='" + config.url + "/images/icon/loading.gif'></img>Loading...");
			$(loadingDiv).css("left", left + "px");
			$(loadingDiv).css("top", top + "px");
			loadingDiv.style.zIndex = '9999';
			loadingDiv.style.position = 'absolute';
		}

		/**
		 * 关闭加载图片层
		 */
		function closeLoading(config) {
			var loadingDiv = document
					.getElementById($(config.currentDomSelect).attr("id") + 'LoadingDiv');
			if (loadingDiv) {
				$(loadingDiv).css("left", "-9999px");
				$(loadingDiv).css("top", "-9999px");
			}
		}

		function getGridTop(config, bottomDistance) {
			var gridObj = document.getElementById(config.ajaxDivId);
			if (gridObj.offsetWidth > bottomDistance) {
				return gridObj.offsetWidth;
			}
		}

		/**
		 * 填充值至文本框
		 */
		function setInputValue(domObj, config) {
			var record = domObj.record;
			if(record && $.isArray(record)){
				var idValue = $.trim(record[0]);
				var code = $.trim(record[1]);
				
// $(config.currentDomSelect).val(code);
// $("#" + config.hiddenId).val(code);
				closeDiv($("#" + config.ajaxDivId), config);
				eval($.StringUtils.firstUpperCase(config.commonServiceName))["loadByID"](config.className, idValue, function(port) {
					autoFillValues(port, config, null);
					if (config.afterSelectJsFunc
							&& config.afterSelectJsFunc != 'null') {
						eval(config.afterSelectJsFunc + '()');
					}
					// 选择后处理事件
					if($.isFunction(config.afterSelectRow)){
						config.afterSelectRow.call($(config.currentDomSelect)[0],port);
					}
				});
				if(config.currentDomSelect){
					$(config.currentDomSelect).focus();
				}
				config.query = true;
				config.isFromSelect = true; // filipo 20100304
				document.onclick = null; // filipo 20100304
			}
		}

		function createHidden(config, idValue) {
			var hiddenObj = document.getElementById($(config.currentDomSelect).attr("id") + "_"
					+ config.hiddenId);
			if (!hiddenObj) {
				hiddenObj = document.createElement('input');
				hiddenObj.type = "hidden";
				hiddenObj.id = $(config.currentDomSelect).attr("id") + "_" + config.hiddenId;
				document.body.appendChild(hiddenObj);
			}
			hiddenObj.value = idValue;
		}

		function autoFillValues(port, config, code) {
			if(config.costomFillFunc){
				eval(config.costomFillFunc+"(port)");
			}else if (!config.autoFill){
				$("#" + config.hiddenId).val(code);				
			}else {
				var arrayAutoFill = config.autoFill.split(',');
				for ( var index = 0; index < arrayAutoFill.length; index++) {
					var autoFill = arrayAutoFill[index].split('|');
					var fieldId = autoFill[0];
					var arrayFieldName = autoFill[1].substring(1,
							autoFill[1].length - 1);
					var fieldName = arrayFieldName.split('_');
					var fieldValue = '';
					for ( var j = 0; j < fieldName.length; j++) {
						fieldValue = fieldValue + port[fieldName[j]] + '/';
					}
					fieldValue = fieldValue.substring(0, fieldValue.length - 1);
					if (fieldValue == null || fieldValue == undefined
							|| fieldValue == 'UNDEFINED'
							|| fieldValue == 'undefined' || fieldValue == 'NULL'
							|| fieldValue == 'null') {
						fieldValue = ''
					}
					var valueField = $("#" + fieldId);
					var displayField = $("#" + fieldId + "_code_name_display");

					if (valueField) {
						//改变存储的值避免引发change事件
						
						if(valueField.length > 0)
							jQuery._data(valueField[0], "_change_data", fieldValue); 
						valueField.val(fieldValue);
					}
					if (displayField) {
						displayField.val(fieldValue);
					}
				}
			}
				
			
		}

		/**
		 * 给整个页面添加点击事件 用于用户点击下拉框外的区域时隐藏下拉框
		 */
		function OnDocClick(e, inputDiv, displayDiv, config) {
			var x, y, t, b, l, r;
			var d;
			var bodyScrollLeft,bodyScrollTop;
			if(!window.XMLHttpRequest){
				bodyScrollLeft = document.body.scrollLeft;
				bodyScrollTop = document.body.scrollTop;
			}else{
				bodyScrollLeft = document.documentElement.scrollLeft;
				bodyScrollTop = document.documentElement.scrollTop;
			} 
			if (window.event) {
				e = window.event;
				x = e.clientX + bodyScrollLeft
						- document.body.clientLeft;
				y = e.clientY + bodyScrollTop
						- document.body.clientTop;
			} else {
				x = e.pageX;
				y = e.pageY;
			}
			t = inputDiv.offset().top;
			l = inputDiv.offset().left;
			b = t + inputDiv.height();
			r = l + inputDiv.width();
			if (x >= l && x < r && y >= t && y < b) {
				return;
			}
			t = displayDiv.offset().top;
			l = displayDiv.offset().left;
			b = t + displayDiv.height();
			r = l + displayDiv.width();
			if (x >= l && x < r && y >= t && y < b) {
				return;
			}
			// 截取空字符串
			String.prototype.Trim = function(){ 
				return this.replace(/(^\s*)|(\s*$)/g, ""); 
			}
			// 限制为不可以输入
			if (config.canNotInput && config.isAjaxDivHide
					&& config.isFromSelect != null && !config.isFromSelect) {
				if ($(config.currentDomSelect).val() != '') {
					checkRecordExist(config);
				}
			} else if (!config.canNotInput) {
				closeDiv(displayDiv, config);
			}

			closeDiv(displayDiv, config);
			config.query = true;
			
			var target = $.EventUtils.getTargetByEvent(e);
			if(target) target.onclick = null;
			document.onclick = null;//清除掉联想控件绑定到document.onclick上的事件，不知道为什么会用这种方式绑定，应急处理有待检查 林丹丹2012-09-06
		}
		/**
		 * 按回车出现下拉内容，不选择，按tab光标跳转到下一个控件，下拉框层不消失 huanglp 20100317
		 */
		function tabKeyCloseDiv(e, config) {
			var kc = getKeyCode(e);
			if (kc == 9) { // Tab
				if (!config.isAjaxDivHide && config.isFromSelect != null && !config.isFromSelect) {
					String.prototype.Trim = function() {
						return this.replace(/(^\s*)|(\s*$)/g, "")
					};
					checkRecordExist(config)
				}
				if(config.isAjaxDivHide && config.isFromSelect != null && !config.isFromSelect){
					$(config.currentDomSelect).val("");
				}
				closeDiv($("#" + config.ajaxDivId), config);
				config.index = -1;
				config.query = true;
				if ($(config.currentDomSelect).val() == "") {
					config.isFromSelect = true;
				}
			}
		}
		function getKeyCode(e) {
			var msie = (document.all) ? true : false;
			var keyCode;
			if (!msie)
				keyCode = e.which;
			else
				keyCode = event.keyCode;
			return keyCode;
		}
		
		function checkRecordExist(config){
			var currentDomValue = $(config.currentDomSelect).val();
			var oddRows = $(config.currentDomSelect + "AutoSelectTableId tr.odd");
			var existFlag = false;
			var displayFieldLength = config.displayField.split(",").length;
// var oddIndex = 0;
			oddRows.each(function(oddIndex,domObj) {
				var record = oddRows[oddIndex];
				for(var fl=0;fl<displayFieldLength;fl++){
					if ($.trim(record.cells[fl].innerText) == $.trim(currentDomValue)) {
						existFlag = true
						break;
					}
				}
				if(existFlag === true){
					return;
				}
// oddIndex = oddIndex + 1
			});
			if(existFlag === false){
				var highlightRows = $(config.currentDomSelect + "AutoSelectTableId tr.autoHighlight");
	// var highIndex = 0;
				highlightRows.each(function(highIndex,domObj) {
					var record = highlightRows[highIndex];
					for(var fl=0;fl<displayFieldLength;fl++){
						if ($.trim(record.cells[fl].innerText) == $.trim(currentDomValue)) {
							existFlag = true
							break;
						}
					}
					if(existFlag === true){
						setInputValue(record, config);
					}
	// highIndex = highIndex + 1
				});
			}
			if (!existFlag && config.canNotInput === true) {
				$(config.currentDomSelect).val("")
				$(config.currentDomSelect).trigger("change");
			}else{
				config.isFromSelect = true;
			}
		}
		/**
		 * 错误信息提示
		 */
		function errorPrompt(config,response){
			// 1.关闭等待条
			closeLoading(config);
			// 2.组装信息层
			var errorPromptDivHtml = '<div style="width:100%;overflow:auto;background-color:#F5F5F5;" id="'+config.id+'ErrorPromptDiv">';
			var showHtml = $.atSelect.ajax.error+"(<a href='#'><span id='"+config.id+"ErrorPromptDetailsTitle'>"+$.atSelect.ajax.errorDetails+"</span></a>)</br>&nbsp;&nbsp;&nbsp;&nbsp;";
			showHtml += $.atSelect.ajax.status+response.status+"</br>&nbsp;&nbsp;&nbsp;&nbsp;";
			showHtml += $.atSelect.ajax.statusText+response.statusText+"</br>&nbsp;&nbsp;&nbsp;&nbsp;";
			showHtml += '<div id="'+config.id+'ErrorPromptDetails" style="display:none;">'+response.responseText+'</div>';
			errorPromptDivHtml += showHtml+'</div>';
			// 3.将信息层放入弹出层
			openDiv(config, errorPromptDivHtml);
			// 4.查看明细
			$("#"+config.id+"ErrorPromptDetailsTitle").toggle(
				function(e){
					$("#"+config.id+"ErrorPromptDetails").show();
				},
				function(e){
					$("#"+config.id+"ErrorPromptDetails").hide();
				}
			)
		}
	})(jQuery);
} else {
	/* 回车联想模式 */
	(function($) {
		$.fn.autoSelect = function(config) {
			return this.each(function(){
				/**
				 * 防止重复初始化
				 */
				if(this.completed){return;}
				
				if(!$(this).hasClass("associateDefault")){
					$(this).addClass("associateDefault");
				}
				
				$(this).attr("autocomplete", "off");
				
				config.id = this.id;
				config.currentDomSelect = "#"+this.id;
				config.ajaxDivId = "autoSelectDiv"+this.id;
				config.width = config.width||350;
				config.filter = config.filter||"";
				config.condition = config.condition || "likeeic";
				config.searchKeyCode = config.searchKeyCode || 13;// 默认回车查询
				config.whereCondition =  config.whereCondition || "";
				config.index=-1;
				config.commonServiceName = config.commonServiceName || "commonService";
				config.inputValueAgain = false;   // 标识选择后是否重新输入
						
				config.ajaxPara = {
					className:config.className,
					sortFields : config.sortFields||"",
					fieldNames : config.displayField ||"",
					queryField : config.queryField || "",
					displayNum : config.displayNum < 1 ? 10 : config.displayNum,
					filter : config.filter,
					parentFieldName : config.parentFieldName||"",
					childFieldName : config.childFieldName||"",   // 子表字段名
																	// filipo
																	// 20100423
					childFieldValue : config.childFieldValue||"",   // 子表字段值
					childFieldValueType : config.childFieldValueType||"",  // 子表字段类型。如字符型，等。filipo
																			// 20100423
					childFieldCondition : config.childFieldCondition||"",  // 匹配表达式
																			// filipo
																			// 20100423
					childFromAssociateCollection : config.childFromAssociateCollection||"", // 子表集名称（主表实体中对应的子表集）
																							// filipo
																							// 20100423
					displayFieldI18n : config.displayFieldI18n || "", // 列头国际化配置
																		// filipo
																		// 20100630
					displayValueMap : config.displayValueMap ||"", // 显示内容转换
																	// filipo
																	// 20100630
					isWhereCondition : config.isWhereCondition || "false", // 依赖whereCondition
																			// 组装的条件标识(20110113xiezm)[针对Fros
																			// 对复杂条件组装不支持，如：多字段的and、or]
					commonServiceName : config.commonServiceName
				};
				
				config.query = true;			// 默认按回车查询 add by liaoqs
				config.isAjaxDivHide = true;	// 标识div层是否隐藏 gxl 20090826
				config.isFromSelect = false;    // 标识是否选择了下拉框中的数据 filipo
												// 20100304
				config.inputValueAgain = false;   // 标识选择后是否重新输入
				config.thisId = this.id;
				
				$(this).keyup(function(e){
					// 防止立即加载
					window.clearTimeout(config.keyupTimeobj);
					config.keyupTimeobj = window.setTimeout(function(){
						if(config.keyupTimeobj == null){
							return;
						}
						var kc = e.keyCode
						if(kc==40){						
							var tableSize = $("#"+config.ajaxDivId+" tr").size()-1;
							if(config.index==tableSize-1)
								config.index=0;
							else
								config.index++;
							$("#"+config.ajaxDivId+" tr.autoHighlight").trigger("mouseout");
							$("#"+config.ajaxDivId+" tr.odd:eq("+config.index+")").trigger("mouseover");
						}else if(kc==38){
							var tableSize = $("#"+config.ajaxDivId+" tr").size()-1;
							if(config.index==-1)
								config.index=tableSize-1;
							else
								config.index--;	
							$("#"+config.ajaxDivId+" tr.autoHighlight").trigger("mouseout");
							$("#"+config.ajaxDivId+" tr.odd:eq("+config.index+")").trigger("mouseover");
						}else if(kc==9){
							return;
						}else if(kc==config.searchKeyCode || kc ==13){
							// 增加按回车键后选择文本，add by liuzg.
							if(!config.query && kc == 13 || (config.searchKeyCode !=13 && kc == 13) )
							{ 
								
								if(config.index!=-1){
									var domObj=$(config.currentDomSelect+"AutoSelectTableId tr.autoHighlight").get(0);
									if(domObj)
										setInputValue(domObj,config);
									else{
	// $(config.currentDomSelect).val("")
	// $(config.currentDomSelect).trigger("change");
										closeDiv($("#" + config.ajaxDivId), config);
									}
									config.index = -1;					
								}else{
	// $(config.currentDomSelect).val("")
	// $(config.currentDomSelect).trigger("change");
								}
								config.query = true;
	
	// config.isFromSelect = true; // filipo 20100304
	// config.isAjaxDivHide = true; // gxl 20090826
	// config.inputValueAgain = false; // gxl 20090826
							}else{				
								getSelectHtml(config);
								return false;
							}			
						}else{
							if(config.characterNum && $(config.currentDomSelect).val().length>=config.characterNum)
								getSelectHtml(config);
							if($(config.currentDomSelect).val()==''){
								closeDiv($("#" + config.ajaxDivId), config);
							}
							config.query = true;
							config.inputValueAgain = true;
						}
					},1000);
				}).change(function(){
					config.isAjaxDivHide = false;
					if(config.canNotInput === true){
						config.isFromSelect = false;
						config.inputValueAgain = true;
					}
					if ($.trim(this.value) == "") {
						$("#" + config.hiddenId).val("");
						$(config.syncClearControls).val("");
					}
				});
				
//				// 如果限制为不可以输入 gxl 20090826
//				$(this).blur(function(){
//					window.clearTimeout(config.keyupTimeobj);
//					if(config.canNotInput && $(this).val().length != 0){
//						// 点击行的config.isFromSelect
//						// 和下拉层的config.isAjaxDivHide
//						// 和当前联想控件输入框 的失焦 事件同时 触发
//						// 导致有时间差 需要延迟判断是否清空输入的值
//						function _cleanValue(){
//							if($(config.currentDomSelect).val() 
//									&& $(config.currentDomSelect).val().length > 0 
//									&& config.isFromSelect === false 
//									&& config.isAjaxDivHide === true 
//									&& config.inputValueAgain === true){
//								config.isFromSelect == true;
//								$(config.currentDomSelect).val("")
//								$(config.currentDomSelect).trigger("change");
//								window.clearInterval(config.timeObj);
//								config.timeObj = null;
//							}
//							else {
//								if($(config.currentDomSelect).val().length == 0){
//									window.clearInterval(config.timeObj);
//									config.timeObj = null;
//								}
//							}
//						}
//				
//						if(config.timeObj == null)
//							config.timeObj = window.setInterval(_cleanValue,50);
//					}
//				});
//				
				var hiddenField = $("#" + config.hiddenId);
				/*
				 * hiddenField.hide(); if(config.showHiddenField){
				 * hiddenField.show();
				 * hiddenField.attr("style","background-color:
				 * #EFEFEF;width:35px;margin-left:-6px;")
				 * hiddenField.attr("readonly","true");
				 * $(this).attr("style","width:85px"); }
				 * if(config.loadRecord){//以下为了编缉的时候为框付值 var input =
				 * $("#"+config.hiddenId)[0]; if(input){
				 * CommonService.findByProperty(config.className,input.name,input.value,function(obj){
				 * if( obj[0] && config.currentDom ) config.currentDom.value =
				 * eval("obj[0]."+config.currentDom.name); }); } }
				 */
				if(config.inTable){// 保存数据
				 	var queryInfo = {
						listType : 'list',
						conjunctionType : 'and',
						queryObjectName : config.className
					};
					var fields = [];   
					var filterFields = config.filter.split(",");
					for (var index = 0; index < filterFields.length; index++) {
						var filterField = filterFields[index].split("|");
						var field = {};
						field.condition = "eq";
						field.fieldName = filterField[0];
						field.fieldValueString = filterField[1];
						fields[fields.length] = field;
					}
					queryInfo.fields = fields;
					eval($.StringUtils.firstUpperCase(config.commonServiceName))["findByQuery"](queryInfo,function(result){
						$(config.currentDomSelect)[0].source = result;
					});
				}
				if(window.addEventListener){
					if(hiddenField[0])
						hiddenField[0].addEventListener('DOMAttrModified', function(){
							$(config.currentDomSelect).attr("title", this.value);
						}, false);
				}else{
					if(hiddenField[0]){
						hiddenField[0].attachEvent("onpropertychange",function(){
							$(config.currentDomSelect).attr("title", this.value);
						});
					}
				}
				
				this.config = _config;
				this.completed = true;
			});
		};

		/**
		 * ajax请求，从后台获取需要的数据
		 */
		function getSelectHtml(config){
			if(config.ajaxPara.fieldValue !== $(config.currentDomSelect).val()){
				config.ajaxPara.currPage = 1;
			}
			if(document.getElementById(config.parentId) 
					&& document.getElementById(config.parentId).value != '' 
// && document.getElementById(config.parentId+"_"+config.parentHiddenId))
					&& document.getElementById(config.parentHiddenId))
			{
// config.parentHiddenValue =
// $("#"+config.parentId+"_"+config.parentHiddenId).val();
				config.parentHiddenValue = $("#"+config.parentHiddenId).val();
				config.ajaxPara.parentFieldValue = config.parentHiddenValue;
			}else{
				config.ajaxPara.parentFieldValue = '';
			}
			config.ajaxPara.selectId = $(config.currentDomSelect).attr("id");
			config.ajaxPara.fieldValue = $(config.currentDomSelect).val();
			config.ajaxPara.condition = config.condition;
			config.ajaxPara.whereCondition = config.whereCondition;
			startLoading(config);
			$.ajax({
				type : "POST",
				url : config.url+"/autoSelect.do?method=auto",
				timeout : 20000,
				error : function(response) {
					errorPrompt(config,response);
				},
				data : config.ajaxPara,
				success : function(msg) {
					openDiv(config,msg);
					closeLoading(config);
					if(config.colWidths){
						var widths = config.colWidths.split(",");
						var headerRows  = $(config.currentDomSelect+"AutoSelectTableId td.tableHeader");
						headerRows.each(function(index,domObj){
							$(domObj).width(widths[index]);
						});
					}
					var rows = $(config.currentDomSelect+"AutoSelectTableId tr.odd");
					rows.each(function(index,domObj){
						$(domObj).bind("click",function(){					
							setInputValue(domObj,config);
							config.isFromSelect = true;   // filipo 20100304
						});
					});
					$("#"+config.ajaxDivId+" tr.odd:eq(0)").trigger("mouseover");
					var totalNum = $(config.currentDomSelect+"totalNumId").val();
					if(parseInt(totalNum) && parseInt(totalNum)>0)
						config.index=0;
					else
						config.index=-1;

					bindEvent(config);
					setImgDisabled(config);
				}
			});
		};
		/**
		 * 分页后事件
		 */
		function setImgDisabled(config){
			if(config.currPage == 1){
				$(config.currentDomSelect+"firstPage").attr("src", config.url+"/scripts/widgets/autoSelect/images/firstPageDisabled.gif");
				$(config.currentDomSelect+"firstPage").attr("title", "");
				$(config.currentDomSelect+"firstPage").attr("alt", "");
				$(config.currentDomSelect+"prevPage").attr("src", config.url+"/scripts/widgets/autoSelect/images/prevPageDisabled.gif");
				$(config.currentDomSelect+"prevPage").attr("title", "");
				$(config.currentDomSelect+"prevPage").attr("alt", "");
			}
			if(config.currPage == config.totalPage){
				$(config.currentDomSelect+"nextPage").attr("src", config.url+"/scripts/widgets/autoSelect/images/nextPageDisabled.gif");
				$(config.currentDomSelect+"nextPage").attr("title", "");
				$(config.currentDomSelect+"nextPage").attr("alt", "");
				$(config.currentDomSelect+"lastPage").attr("src", config.url+"/scripts/widgets/autoSelect/images/lastPageDisabled.gif");
				$(config.currentDomSelect+"lastPage").attr("title", "");
				$(config.currentDomSelect+"lastPage").attr("alt", "");
			}
		}
		/**
		 * 绑定分页事件
		 */
		function bindEvent(config) {
			var currPage = $(config.currentDomSelect+"currPageId");
			var totalPage = $(config.currentDomSelect+"totalPageId");
			var totalNum = $(config.currentDomSelect+"totalNumId");
			if (currPage[0])
				config.currPage = parseInt(currPage[0].value);
			if (totalPage[0])
				config.totalPage = parseInt(totalPage[0].value);
			if(totalNum[0])
				config.totalNum=parseInt(totalNum[0].value);
			if(config.currPage != 1){	
				$(config.currentDomSelect+"firstPage").click(function(e) {
					config.ajaxPara.currPage = 1;
					getSelectHtml(config);
				});
				$(config.currentDomSelect+"prevPage").click(function(e) {
					config.ajaxPara.currPage = config.currPage - 1;
					getSelectHtml(config);
				});
			}
			if(config.currPage != config.totalPage){
				$(config.currentDomSelect+"nextPage").click(function(e) {
					config.ajaxPara.currPage = config.currPage + 1;
					getSelectHtml(config);
				});
				$(config.currentDomSelect+"lastPage").click(function(e) {
					config.ajaxPara.currPage = config.totalPage;
					getSelectHtml(config);
				});
			}
			var fun = null;
			$(config.currentDomSelect+"gotoPageInput").bind("focus",function(event){
				fun = document.onclick;
				document.onclick = null;
			});
			$(config.currentDomSelect+"gotoPageInput").bind("blur",function(event){
				document.onclick = fun;
			});
			
			$(config.currentDomSelect+"gotoPageInput").bind("keyup",function(event){
				var e=event||window.event;
				if(e.keyCode==13){
					$(config.currentDomSelect+"gotoPage").trigger("click");
				}
			});
			
			$(config.currentDomSelect+"gotoPage").click(function(e) {
				var value = $(config.currentDomSelect+"gotoPageInput")[0].value;
				if (value) {
					if(parseInt(value) != config.currPage && parseInt(value) > 0 && parseInt(value) <= config.totalPage){
						config.ajaxPara.currPage = value;
						getSelectHtml(config);
					}
				}
			});
		}

		/**
		 * 打开下拉框内容
		 */
		function openDiv(config,msg) {
			var obj = $(config.currentDomSelect)[0];
			var divId = config.ajaxDivId;
			var width = config.width;
			var opDiv = document.getElementById(divId);
			var offset = $(obj).offset();
			if (!opDiv) {
				opDiv = document.createElement('div'); // 创建层
				opDiv.id = divId;
				document.body.appendChild(opDiv);
			}
			$("#" + config.ajaxDivId).html(msg);
			var w = document.body.offsetWidth - offset.left;
			var left = null;
			if (w < width) {
				left = offset.left + obj.offsetWidth - width;
			} else {
				left = offset.left;
			}
			var bottomDistance=document.documentElement.clientHeight-offset.top;
			var topDistance=offset.top-obj.offsetHeight;
			var gridObj=document.getElementById(config.ajaxDivId);
			var top = offset.top + $(obj).height() + 5;
			if(gridObj.offsetHeight+5>bottomDistance && topDistance>gridObj.offsetHeight)
				top=offset.top-gridObj.offsetHeight-3;
			$(opDiv).css("left",left+"px");
			$(opDiv).css("top",top+"px");
			$(opDiv).css("width",width+"px");
			opDiv.style.zIndex = '9999';
			opDiv.style.position = 'absolute';
			opDiv.style.cursor = 'default';
			document.onclick = function(e) {
				$(config.currentDomSelect+"gotoPageInput").val("");
				OnDocClick(e, $(obj), $(opDiv),config);
				// config.isAjaxDivHide = true; // gxl 20090826
			};
			config.query=false;
			config.isAjaxDivHide = false;  // filipo 20100304
			//多tab点击需要关闭
			$("a").bind("click",function(e){
				checkRecordExist(config);
				OnDocClick(e, $(obj), $(opDiv), config);
			});
		}

		/**
		 * 关闭下拉框
		 */
		function closeDiv(displayDiv, config){
			$(displayDiv).css("left","-9999px");
			$(displayDiv).css("top","-9999px");
			config.isAjaxDivHide = true;   // filipo 20100304
		}

		/**
		 * 打开加载图片层
		 */
		function startLoading(config){
			var obj = $(config.currentDomSelect)[0];
			var offset = $(obj).offset();
			var top = offset.top + $(obj).height() + 5;
			var left = offset.left;
			var currentDomId = $(config.currentDomSelect).attr("id")
			var loadingDiv=document.getElementById(currentDomId+'LoadingDiv');
			if(!loadingDiv){
				loadingDiv=document.createElement('div');
				loadingDiv.id=currentDomId+'LoadingDiv';
				document.body.appendChild(loadingDiv);
			}
			$(loadingDiv).html("<img src='"+config.url+"/images/icon/loading.gif'></img>Loading...");
			$(loadingDiv).css("left",left+"px");
			$(loadingDiv).css("top",top+"px");
			loadingDiv.style.zIndex = '9999';
			loadingDiv.style.position = 'absolute';
		}

		/**
		 * 关闭加载图片层
		 */
		function closeLoading(config){
			var loadingDiv=document.getElementById($(config.currentDomSelect).attr("id")+'LoadingDiv');
			if(loadingDiv){
				$(loadingDiv).css("left","-9999px");
				$(loadingDiv).css("top","-9999px");
			}
		}
		function getGridTop(config,bottomDistance){
			var gridObj=document.getElementById(config.ajaxDivId);
			if(gridObj.offsetWidth>bottomDistance){
				return gridObj.offsetWidth;
			}
		}

		/**
		 * 填充值至文本框
		 */
		function setInputValue(domObj,config){
			var record = domObj.record;
			if(record && $.isArray(record)){
				var idValue = $.trim(record[0]);
				var code = $.trim(record[1]);
				
// $(config.currentDomSelect).val(code);
// $("#" + config.hiddenId).val(code);
				closeDiv($("#" + config.ajaxDivId), config);
				eval($.StringUtils.firstUpperCase(config.commonServiceName))["loadByID"](config.className, idValue, function(port) {
					autoFillValues(port, config, null);
					if (config.afterSelectJsFunc
							&& config.afterSelectJsFunc != 'null') {
						eval(config.afterSelectJsFunc + '()');
					}
					// 选择后处理事件
					if($.isFunction(config.afterSelectRow)){
						config.afterSelectRow.call($(config.currentDomSelect)[0],port);
					}
				});
				if(config.currentDomSelect){
					$(config.currentDomSelect).focus();
				}
				config.query = true;
				config.isFromSelect = true; // filipo 20100304
				document.onclick = null; // filipo 20100304
			}
		}
		function createHidden(config,idValue){
			var hiddenObj = document.getElementById($(config.currentDomSelect).attr("id")+"_"+config.hiddenId);
			if(!hiddenObj)
			{
				hiddenObj = document.createElement('input');
				hiddenObj.type = "hidden";
				hiddenObj.id = $(config.currentDomSelect).attr("id")+"_"+config.hiddenId;
				document.body.appendChild(hiddenObj);
			}
			hiddenObj.value = idValue;
		}
		function autoFillValues(port,config,code){
			if(config.costomFillFunc){
				eval(config.costomFillFunc+"(port)");
			}else if (!config.autoFill){
				$("#" + config.hiddenId).val(code);				
			}else {
				var arrayAutoFill = config.autoFill.split(',');
				for(var index = 0; index < arrayAutoFill.length; index ++){
					var autoFill = arrayAutoFill[index].split('|');
					var fieldId = autoFill[0];
					var arrayFieldName = autoFill[1].substring(1, autoFill[1].length -1);
					var fieldName = arrayFieldName.split('_'); 
					var fieldValue = '';
					for(var j = 0; j < fieldName.length; j ++){
						fieldValue = fieldValue + port[fieldName[j]] + '/'; 
					}
					fieldValue = fieldValue.substring(0, fieldValue.length-1);
					if(fieldValue == null || fieldValue == undefined
						|| fieldValue == 'UNDEFINED' || fieldValue == 'undefined'
						|| fieldValue == 'NULL' || fieldValue == 'null'){
						fieldValue = ''
					}
					var valueField = $("#"+fieldId);
					var displayField = $("#"+fieldId+"_code_name_display");
					
					if(valueField){
						valueField.val(fieldValue);
					}
					if(displayField){
						displayField.val(fieldValue);
					}
				}
			}
			
		}
		/**
		 * 给整个页面添加点击事件 用于用户点击下拉框外的区域时隐藏下拉框
		 */
		function OnDocClick(e,inputDiv,displayDiv,config){
			var x, y, t, b, l, r;
				var d;
				if (window.event) {
					e = window.event;
					x = e.clientX + document.body.scrollLeft - document.body.clientLeft;
					y = e.clientY + document.body.scrollTop - document.body.clientTop;
				} else {
					x = e.pageX;
					y = e.pageY;
				}
				t = inputDiv.offset().top;
				l = inputDiv.offset().left;
				b = t + inputDiv.height();
				r = l + inputDiv.width();
				if (x >= l && x < r && y >= t && y < b) {
					return;
				}
				t = displayDiv.offset().top;
				l = displayDiv.offset().left;
				b = t + displayDiv.height();
				r = l + displayDiv.width();
				if (x >= l && x < r && y >= t && y < b) {
					return;
				}
				
				if($(config.currentDomSelect).val().length == 0){
					closeDiv(displayDiv, config); 
					var currentDomId = $(config.currentDomSelect).attr("id");
					var domId = currentDomId.substring(0, currentDomId
							.indexOf('_code_name_display') >-1 ? currentDomId.indexOf('_code_name_display') : currentDomId.length);
					$('#'+domId).val('');
				}
				
				// 如果限制为不可以输入
				if(config.canNotInput && $(config.currentDomSelect).val().length != 0){
					if(config.isAjaxDivHide&&config.isFromSelect!=null&&!config.isFromSelect || config.inputValueAgain){
						alert('3请从下拉框中选择数据！');
						$(config.currentDomSelect).val("")
						$(config.currentDomSelect).trigger("change");
					}else if(config.isFromSelect!=null&&!config.isFromSelect && !config.inputValueAgain){
						if($(config.currentDomSelect).val() != ""){
							alert('4请从下拉框中选择数据！');
							$(config.currentDomSelect).val("")
							$(config.currentDomSelect).trigger("change");
						}
					}
				}else{
					closeDiv(displayDiv, config);
					var currentDomId = $(config.currentDomSelect).attr("id");
					var domId = currentDomId.substring(0, currentDomId
							.indexOf('_code_name_display') >-1 ? currentDomId.indexOf('_code_name_display') : currentDomId.length);
					$('#'+domId).val($(this).val());
				}
				
				closeDiv(displayDiv, config);　// 隐藏div
				config.query = true;
				$.EventUtils.getTargetByEvent(e).onclick = null;
		}

		function getKeyCode(e){
			var msie = (document.all) ? true : false; 
		  	var keyCode; 
		    if(!msie)
		        keyCode=e.which; 
		    else
		        keyCode=event.keyCode; 
		    return keyCode;
		}
		
		/**
		 * 错误信息提示
		 */
		function errorPrompt(config,response){
			// 1.关闭等待条
			closeLoading(config);
			// 2.组装信息层
			var errorPromptDivHtml = '<div style="width:100%;overflow:auto;background-color:#F5F5F5;" id="'+config.id+'ErrorPromptDiv">';
			var showHtml = $.atSelect.ajax.error+"(<a href='#'><span id='"+config.id+"ErrorPromptDetailsTitle'>"+$.atSelect.ajax.errorDetails+"</span></a>)</br>&nbsp;&nbsp;&nbsp;&nbsp;";
			showHtml += $.atSelect.ajax.status+response.status+"</br>&nbsp;&nbsp;&nbsp;&nbsp;";
			showHtml += $.atSelect.ajax.statusText+response.statusText+"</br>&nbsp;&nbsp;&nbsp;&nbsp;";
			showHtml += '<div id="'+config.id+'ErrorPromptDetails" style="display:none;">'+response.responseText+'</div>';
			errorPromptDivHtml += showHtml+'</div>';
			// 3.将信息层放入弹出层
			openDiv(config, errorPromptDivHtml);
			// 4.查看明细
			$("#"+config.id+"ErrorPromptDetailsTitle").toggle(
				function(e){
					$("#"+config.id+"ErrorPromptDetails").show();
				},
				function(e){
					$("#"+config.id+"ErrorPromptDetails").hide();
				}
			)
		}
	})(jQuery);
}
